package backtracking.nowcoder.medium;

import java.util.Scanner;

/**
 * @author bruin_du
 * @description 走迷宫
 * @date 2022/7/3 16:17
 **/
public class Maze {
    static int steps = 100;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNextLine()) {
            char[][] arr = new char[10][10];
            for (int i = 0; i < 10; i++) {
                String str = in.nextLine();
                for (int j = 0; j < 10; j++) {
                    arr[i][j] = str.charAt(j);
                }
            }
            runAMaze1(arr,0,1,0);
            runAMaze2(arr,0,1,0);
            if(steps == 100) {
                runAMaze(arr,0,1,0);
            }
            System.out.println(steps);
            steps = 100;
        }
    }

    private static void runAMaze1(char[][] arr, int i, int j,int count) {
        if(i < 0 || i > 9 || j < 0 || j > 9 || arr[i][j] != '.') {
            return;
        }
        if(i == 9 && j == 8) {
            steps = Math.min(steps,count);
            return;
        }
        runAMaze1(arr, i + 1, j, count + 1);
        runAMaze1(arr, i, j + 1, count + 1);
    }

    private static void runAMaze2(char[][] arr, int i, int j,int count) {
        if(i < 0 || i > 9 || j < 0 || j > 9 || arr[i][j] != '.') {
            return;
        }
        if(i == 9 && j == 8) {
            steps = Math.min(steps,count);
            return;
        }
        runAMaze2(arr, i, j + 1, count + 1);
        runAMaze2(arr, i + 1, j, count + 1);
    }

    private static void runAMaze(char[][] arr, int i, int j,int count) {
        if(i < 0 || i > 9 || j < 0 || j > 9 || arr[i][j] != '.') {
            return;
        }
        if(i == 9 && j == 8) {
            steps = Math.min(steps,count);
            return;
        }
        arr[i][j] = '#';
        runAMaze(arr, i + 1, j, count + 1);
        runAMaze(arr, i, j + 1, count + 1);
        runAMaze(arr, i - 1, j, count + 1);
        runAMaze(arr, i, j - 1, count + 1);
        arr[i][j] = '.';
    }
}
